﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace BIStudio.Framework.Utils
{
    public class SendData
    {
        #region "字段属性"
        private static SendData _intsance;

        public static SendData Intsance
        {
            get { return SendData._intsance; }
            set { SendData._intsance = value; }
        }
        #endregion

        #region "构造函数"
        public SendData()
        {

        }

        static SendData()
        {
            _intsance = new SendData();
        }
        #endregion

        #region "方法事件"
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="strPost">Post字符串</param>
        /// <param name="strencoding">编码格式</param>
        /// <param name="contentType">Content-typeHTTP 标头的值</param>
        /// <param name="timeout">请求超时时间</param>
        /// <returns>响应报文</returns>
        public string SendRequest(string url, string strPost, StrEncodingEnum strencoding = StrEncodingEnum.UTF8, ContentTypeEnum contentType= ContentTypeEnum.ApplicationJson,int timeout = 3000)
        {
            DownloadFile _downloadfile = new DownloadFile();
            _downloadfile.Url = url;
            _downloadfile.StrEncoding = ALEnumDescription.GetFieldText(strencoding) ;
            _downloadfile.ContentType = ALEnumDescription.GetFieldText(contentType);
            _downloadfile.StrPost = strPost;
            _downloadfile.Timeout = timeout;
            _downloadfile.Start();
            return _downloadfile.HtmlText;
        }
        /// <summary>
        /// MD5加密程序（32位小写）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string md5(string str)
        {
            byte[] result = Encoding.Default.GetBytes(str);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            String md = BitConverter.ToString(output).Replace("-", "");
            return md.ToLower();
        }

        #endregion

        #region "数据转换"

        /// <summary>
        /// 可打印的byte[]字符串转换回byte[]
        /// </summary>
        /// <param name="strvalue">可打印的byte[]字符串</param>
        /// <returns>byte[]</returns>
        public byte[] StringToBytes(string strvalue)
        {
            if (IsBase64String(strvalue))
            {
                return Convert.FromBase64String(strvalue);
            }
            else
            {
                return new byte[0];
            }
        }

        /// <summary>
        /// byte[]转换为可打印的字符串
        /// </summary>
        /// <param name="inArray">byte[]</param>
        /// <returns>string字符串</returns>
        public string BytesToString(byte[] inArray)
        {
            return Convert.ToBase64String(inArray);
        }
        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="strvalue">可打印的Base64 byte[]字符串</param>
        /// <returns></returns>
        public bool IsBase64String(string strvalue)
        {
            //A-Z, a-z, 0-9, +, /, =
            return System.Text.RegularExpressions.Regex.IsMatch(strvalue, @"[A-Za-z0-9\+\/\=]");
        }
        #endregion
    }
}